package com.mobile.collect.activitys;

import android.content.Intent;
import android.view.View;

import com.mobile.collect.R;
import com.mobile.collect.beans.DevicePointInfo;
import com.mobile.collect.beans.SealPointInfo;
import com.mobile.collect.beans.TypeInfo;
import com.mobile.collect.db.EntityManager;
import com.mobile.collect.dialogs.SubmitTipDialog;
import com.mobile.collect.present.PicPointPresent;
import com.mobile.collect.utils.FileUtil;
import com.mobile.collect.utils.XMLUtil;
import com.mobile.collect.view.DimenDialog;
import com.mobile.collect.view.InputDialog;
import com.mobile.collect.view.NumberPop;
import com.mobile.collect.view.TypeDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;
import cn.droidlover.xdroidmvp.base.BaseListActivity;
import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.ListViewChildClickListener;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;
@Deprecated
public class SealingPointActivity_bat extends BaseListActivity<SealPointInfo, PicPointPresent> implements ListViewChildClickListener {
    DevicePointInfo devicePointInfo;
    List<TypeInfo> typeInfoList;
    boolean isUpdate;

    List<SealPointInfo> dataList;

    @Override
    public int getTitleLayoutResId() {
        return 0;
    }

    @Override
    public void initView() {
        super.initView();
        isUpdate = devicePointInfo.get_ID() != null;
        if (isUpdate) {
            titleTv.setText("修改密封点");
        } else {
            titleTv.setText("添加密封点");
        }
        findViewById(R.id.rightIv).setVisibility(View.VISIBLE);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_sealingpoint;
    }

    @Override
    public BaseRecyclerAdapter<SealPointInfo> getAdapter() {
        return new BaseRecyclerAdapter<SealPointInfo>(this) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_sealingpoint;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position) {
                super.bindData(holder, position);
                SealPointInfo pointInfo = getItemByPosition(position);
                holder.setText(R.id.childTypeTv, pointInfo.getChildTypeEn());
                holder.setText(R.id.dimenTv, pointInfo.getDimen());
                holder.setChildOnClick(position, R.id.childTypeTv, SealingPointActivity_bat.this);
                holder.setChildOnClick(position, R.id.dimenTv, SealingPointActivity_bat.this);
                holder.setChildOnClick(position, R.id.delIv, SealingPointActivity_bat.this);
                if (getItemByPosition(position).getCount() == 0) {
                    holder.setText(R.id.numberTv, "");
                } else {
                    holder.setText(R.id.numberTv, "" + pointInfo.getCount());
                }
                if (!pointInfo.isUpdate()) {
                    holder.setChildOnClick(position, R.id.numberTv, SealingPointActivity_bat.this);
                }
            }
        };
    }

    @Override
    public void itemClick(int position) {

    }

    @Override
    public void getDataFromServer() {
        SealPointInfo sealPointInfo;
        if (isUpdate) {
            sealPointInfo = new SealPointInfo();
            sealPointInfo.setUpdate(true);
            sealPointInfo.setCount(1);
            sealPointInfo.setChildTypeEn(devicePointInfo.getChildTypeEn());
            sealPointInfo.setChildType(devicePointInfo.getChildType());
            sealPointInfo.setDimen(devicePointInfo.getDiameter());
            sealPointInfo.setType(devicePointInfo.getType());
            sealPointInfo.setTypeEn(devicePointInfo.getTypeEn());
        } else {
            sealPointInfo = new SealPointInfo();
        }
        mBaseAdapter.add(sealPointInfo);
        XMLUtil xmlUtil = new XMLUtil(FileUtil.getFromAssets(this, "childtype.xml"));
        typeInfoList = xmlUtil.parserChildType();
    }

    @Override
    public void getIntentData() {
        devicePointInfo = getIntent().getParcelableExtra("devicePointInfo");
    }

    @Override
    public PicPointPresent newP() {
        return new PicPointPresent();
    }


    @OnClick({R.id.rightIv, R.id.submitBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rightIv:
                mBaseAdapter.add(new SealPointInfo());
                break;
            case R.id.submitBtn:
                saveSealPoints();
                break;
        }
    }

    private void saveSealPoints() {
        List<DevicePointInfo> newPointList = new ArrayList<>();
        List<SealPointInfo> list = mBaseAdapter.getmList();

        int expend = 0;
        int count = list.size();
        for (int i = 0; i < count; i++) {
            SealPointInfo sealPointInfo = list.get(i);

            if (sealPointInfo.getChildType() == null) {
                showToast("请选择类型");
                return;
            }
            if (sealPointInfo.getDimen() == null) {
                showToast("请选择尺寸");
                return;
            }
            if (sealPointInfo.getCount() == 0) {
                showToast("请选择数量");
                return;
            }

            String childTypeEn = sealPointInfo.getChildTypeEn();
            if (childTypeEn.equals("KP") || childTypeEn.equals("空冷丝堵")) {
                DevicePointInfo pointInfo = getNewDevicePointInfo(sealPointInfo, expend);
                pointInfo.setDeviceCount(sealPointInfo.getCount());
                newPointList.add(pointInfo);
                expend++;
            } else {
                for (int r = 0; r < sealPointInfo.getCount(); r++) {
                    DevicePointInfo pointInfo = getNewDevicePointInfo(sealPointInfo, expend);
                    pointInfo.setDeviceCount(1);
                    expend++;
                    newPointList.add(pointInfo);
                }
            }

        }
        EntityManager.getInstance().savePointInfo(newPointList);
        startActivity(new Intent(this, DevicePointsActivity.class));
    }

    private DevicePointInfo getNewDevicePointInfo(SealPointInfo sealPointInfo, int expend) {
        DevicePointInfo pointInfo;
        if (sealPointInfo.isUpdate()) {
            pointInfo = devicePointInfo;
        } else {
            pointInfo = devicePointInfo.getNewPointInfo();
            pointInfo.set_ID(null);
        }

        pointInfo.setType(sealPointInfo.getType());
        pointInfo.setTypeEn(sealPointInfo.getTypeEn());
        pointInfo.setChildType(sealPointInfo.getChildType());
        pointInfo.setChildTypeEn(sealPointInfo.getChildTypeEn());
        pointInfo.setDiameter(sealPointInfo.getDimen());

        if (!sealPointInfo.isUpdate()) {
            int indexX = expend % 10;
            int indexY = expend / 10;
            pointInfo.setScaleY(0.1f * indexX);
            pointInfo.setScaleX(0.1f * indexY);
        }
        return pointInfo;
    }

    @Override
    public void itemChildClick(int position, int viewId) {
        switch (viewId) {
            case R.id.childTypeTv:
                showTypeDialog(mBaseAdapter.getItemByPosition(position), "组件子类型", position);
                break;
            case R.id.dimenTv:
                showDimenDialog(mBaseAdapter.getItemByPosition(position), "尺寸");
                break;
            case R.id.numberTv:
                SealPointInfo info = mBaseAdapter.getItemByPosition(position);
                String oldTxt = info.getCount() == 0?"":String.valueOf(info.getCount());
                showNumberDialog(info, "数量", oldTxt,0, true);
                break;
            case R.id.delIv:
                SealPointInfo pointInfo = mBaseAdapter.getItemByPosition(position);
                if (pointInfo.isUpdate()) {
                    showDelPointTip(pointInfo);
                } else {
                    mBaseAdapter.remove(pointInfo);
                }

                break;
        }
    }

    private void showNumberDialog(final SealPointInfo info, final String label,  final String oldTxt, int maxCount, boolean onlyNum) {
        NumberPop numberPop = new NumberPop(this, label,oldTxt, new InputDialog.InputCompleteListener() {
            @Override
            public void onInputComplete(String value) {
                info.setCount(Integer.parseInt(value));
                mBaseAdapter.notifyDataSetChanged();
            }
        }, maxCount);
        numberPop.show();
    }

    private void showTypeDialog(final SealPointInfo info, final String label, final int position) {
        TypeDialog numberPop = new TypeDialog(this, label, new TypeDialog.TypeCompleteListener() {
            @Override
            public void onInputComplete(String value) {

            }

            @Override
            public void setType(TypeInfo childTypeInfo) {
                info.setType(childTypeInfo.getTypeName());
                info.setTypeEn(childTypeInfo.getTypeCode());
                info.setChildTypeEn(childTypeInfo.getChildCode());
                info.setChildType(childTypeInfo.getChildName());
                info.setCount(1);
                toAutoFalan(info, position);
                mBaseAdapter.notifyDataSetChanged();
            }
        });
        numberPop.show();
    }

    private void toAutoFalan(SealPointInfo info, int position) {
        SealPointInfo falan = null;
        switch (info.getChildTypeEn()) {
            case "GV":
            case "CV":
            case "BUV":
            case "GLV":
            case "BV": {
                falan = info.getAboutFaLan();
                if (null == falan) {
                    falan = new SealPointInfo();
                    falan.setCount(2);
                    falan.setType("法兰");
                    falan.setTypeEn("F");
                    falan.setChildTypeEn("F");
                    falan.setChildType("法兰");
                }
                info.setAboutFaLan(falan);
            }

        }
        if (falan != null) {
            mBaseAdapter.add(position+1, falan);
        }

    }


    private void showDimenDialog(final SealPointInfo info, final String label) {
        int count = info.getCount();
        String oldTxt = info.getDimen();
        DimenDialog numberPop = new DimenDialog(this, label,oldTxt, new InputDialog.InputCompleteListener() {
            @Override
            public void onInputComplete(String value) {
                info.setDimen(value);
                if (info.getAboutFaLan() !=null) {
                    info.getAboutFaLan().setDimen(value);
                }
                mBaseAdapter.notifyDataSetChanged();
            }
        });
        numberPop.show();
    }

    @Override
    public boolean isNotWorkConnect() {
        return false;
    }

    private void showDelPointTip(final SealPointInfo pointInfo) {
        SubmitTipDialog submitTipDialog = new SubmitTipDialog(this, new SubmitTipDialog.SlectClickListener() {
            @Override
            public void submitClick() {
                EntityManager.getInstance().delDevicePointInfo(devicePointInfo);
                mBaseAdapter.remove(pointInfo);
            }

            @Override
            public void cancelClick() {

            }
        });
        submitTipDialog.display("温馨提示", "是否删除该密封点?", "取消", "删除");
    }
}
